package com.example.gmr.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.gmr.Helper.SharedPreferenceHelper;
import com.example.gmr.R;

import java.util.Map;

public class Main extends AppCompatActivity implements View.OnClickListener {

    //创建控件对象
    private EditText username,password;
    private SharedPreferenceHelper helper;
    private Context context;
    private Button login,register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        context= getApplicationContext();
        helper=new SharedPreferenceHelper(context);
        username=(EditText)findViewById(R.id.username);
        password=(EditText)findViewById(R.id.password);
        login=(Button)findViewById(R.id.login);

        //获取监听
        login.setOnClickListener(this);
        register=(Button)findViewById(R.id.register);
        register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()){
            case R.id.login:
                Map<String,String> data=helper.read();    //获取注册信息
                if(username.getText().toString().equals(data.get("username"))){
                    if (password.getText().toString().equals(data.get("password"))){
                        intent =new Intent(context, Login.class);
                        startActivity(intent);
                    }else {
                        Toast.makeText(Main.this, "输入密码错误，请重新输入!", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(Main.this, "用户名不存在，请重新输入!", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.register:
                intent=new Intent(Main.this,Register.class);
                startActivity(intent);
                Toast.makeText(this,"请注册···",Toast.LENGTH_SHORT).show();
                break;
            default :
                break;
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        Map<String,String> data=helper.read();
        username.setText(data.get("username"));
        password.setText(data.get("password"));
    }
}
